package com.imxiaoyu.wifi.adb.utils;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CmdUtils {

    /**
     * 执行adb命令
     *
     * @param cmd 要执行的命令  参数
     * @return 返回执行结果，区分换行
     */
    public static List<String> runCmd(String cmd) {
        System.out.print("\n执行命令：" + cmd);
        List<String> list = new ArrayList<>();
        Process proc = null;
        BufferedReader reader;
        String line = null;
        Runtime runtime = Runtime.getRuntime();
        try {
            proc = runtime.exec(cmd);

            if (proc != null) {
                reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    if (line.length() > 1) {
                        list.add(line);
                    }
                }
            }

        } catch (Exception e) {
            System.out.print("执行命令:" + cmd + "出错啦！");
            return null;
        }

        System.out.print("执行结果:" + new Gson().toJson(list));
        return list;
    }
}
